﻿using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows;
namespace WPFExcelReport
{
    public class CaseInsensitiveComparer : IComparer<object>
    {
        public int Compare(object x, object y)
        {
            return string.Compare((string)x, (string)y, StringComparison.OrdinalIgnoreCase);
        }
    }
    class MVTEXAPPReport : BaseReport
    {
        #region ----------------PROPERTIES---------------------------------- 
        string DocType;
        #endregion

        public MVTEXAPPReport()
            : base()
        {
            TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\MVT\MVTEXVK.xls");
            if (exAgent == null)
            {
                exAgent = new ExcelX.ExcelLib.ExcelAgent(TemplatePath);
            }
        }
        public override void WriteExcelReport(string fileType)
        {
            strCompany = "MVT";
            Root = @"C:\IntexReport";
            bool IsFTP = false;
            try
            {
                Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
                strCompany = "MVT";
                strExcelFile = @"MVT\MVTEXVK.xls";
                Standard = "EXVV06";
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\MVT\MVTEXVK.xls");
                destfolder = SetFolder(Root, "EXORD");
                DataRow dwOrder = dsSource.Tables["ICC_EXORD"].Rows[0];
                string KRS = dwOrder["VKPAUFKRS"].ToString().Trim();
                string LFD = dwOrder["VKPAUFLFD"].ToString().Trim();
                string newfilename = KRS.Trim() + "-" + Helpers.Helper.GetSubRollNum(LFD);
                exAgent.CheckWorkBook(newfilename);
                ServerShareFile(KRS + LFD, fileType);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        private void FillInEXCEL()
        {
            try
            {
                string value = "";
                int x = 1, y = 1;
                int rown = 2;
                string processed = "";
                int idx = 0;
                int rowstart = 9;
                DataRow dwVKP;
                dwVKP = dsSource.Tables["ICC_EXORD"].Rows[0];
                #region===Header============
                exAgent.SetCellValue(5, 4, dwVKP["CUSTOMER"].ToString().Trim());
                exAgent.SetCellValue(5, 30, dwVKP["VKPAUFKRS"].ToString() + dwVKP["VKPAUFLFD"].ToString());
                exAgent.SetCellValue(6, 4, dwVKP["CHAR1"].ToString().Trim());
                exAgent.SetCellValue(6, 14, dwVKP["CHAR2"].ToString().Trim());
                exAgent.SetCellValue(6, 20, dwVKP["CHAR3"].ToString().Trim());
                exAgent.SetCellValue(6, 30, year + "/" + month + "/" + day );
                #endregion
                int count = dsSource.Tables["ICC_EXORD"].Rows.Count;
                foreach (DataRow row in dsSource.Tables["ICC_EXORD"].Rows)
                {
                    if (row["VKPART"].ToString().Trim() == "") continue;
                    exAgent.SetCellValue(rowstart + rown * idx, 1, row["VKPAUFPOS"].ToString().Trim());
                    exAgent.SetCellValue(rowstart + rown * idx, 3, row["VKPART"].ToString().Trim());
                    exAgent.SetCellValue(rowstart + rown * idx, 7, row["VKPFARBE"].ToString().Trim());
                    exAgent.SetCellValue(rowstart+  rown * idx, 11, row["QUANTITY"].ToString().Trim());
                    exAgent.SetCellValue(rowstart+ rown * idx, 22, row["SPRICE"].ToString().Trim());// + row["CHAR1"].ToString().Trim());
                    exAgent.SetCellValue(rowstart+  rown * idx, 17, row["VWIDTH"].ToString().Trim() + "-" + row["TWIDTH"].ToString().Trim()+"CM");
                    exAgent.SetCellValue(rowstart+  rown * idx, 13, row["GSQM"].ToString().Trim() + "g/sqm");
                    exAgent.SetCellValue(rowstart + rown * idx, 25, row["COMPOSITION"].ToString());
                    //exAgent.SetCellValue(rowstart+  rown * idx, 25, row["GM"].ToString().Trim() + "g/m");
                    //exAgent.SetCellValue(rowstart+  rown * idx, 28, row["YARN"].ToString().Trim() );
                    exAgent.SetCellValue(rowstart + rown * idx + 1, 1, "Remark:");
                    exAgent.SetCellValue(rowstart + rown * idx + 1, 3, row["REMARK"].ToString());
                    //exAgent.SetCellValue(rowstart+ rown * idx+3, 3, "note:"+row["REMARK"].ToString());
                    idx++;
                    if (idx < count)
                    {
                        exAgent.RangeCopy(9, 10, rowstart + rown * idx, rowstart + rown * idx + 1);
                    }
                }
                exAgent.SetCellValue(rowstart+ rown * idx + 1, 23, "Total:" + idx + " items");
                //exAgent.SetCellValue(rowstart+ rown * idx + 4, 23, "Best Regards,");
                //exAgent.SetCellValue(rowstart + rown * idx + 6, 23, "Max Vogue");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }


        private string CreateFile(string orderno)
        {
            string filename = "";
            string sheetName = "";
            string Standard = "EXVV06";
            try
            {
                string pwd = "";
                sheetName = orderno;
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(orderno + ".xls", Standard, orderno.Replace(".xls", ""), pwd);
                filename = exAgent.FileName;
                FillInEXCEL();
                string localFile = exAgent.FileName;
                exAgent.Save();
                exAgent.Close();
                //System.Diagnostics.Process.Start("EXCEL.EXE", localFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
            return filename;
        }

        private void ServerShareFile(string ordernumer, string fileType, DataRow row = null)
        {
            string destfile, tempfile = "";
            string fileabsname = "EX-" + ordernumer;
            filename = destfolder + "\\" + fileabsname + ".xls"; ;
            destfile = filename;
            try
            {
                tempfile = CreateFile(ordernumer);

                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);

                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
    }
}